package com.yi.servicedriveruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yi.internalcommon.constant.CommonStatusEnum;
import com.yi.internalcommon.constant.DriverCarConstants;
import com.yi.internalcommon.dto.*;
import com.yi.internalcommon.responese.OrderDriverResponse;
import com.yi.servicedriveruser.mapper.CarMapper;
import com.yi.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.yi.servicedriveruser.mapper.DriverUserMapper;
import com.yi.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private CarMapper carMapper;

    public ResponseResult testDriverUser() {

        DriverUser driverUser = driverUserMapper.selectById(1);
        return ResponseResult.success(driverUser);
    }

    /**
     * 插入司机信息
     *
     * @param driverUser
     * @return
     */
    public ResponseResult addDriverUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);
        //初始化 司机工作状态表
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return ResponseResult.success("");
    }

    /**
     * 修改司机信息
     *
     * @param driverUser
     * @return
     */
    public ResponseResult updateDriverUser(DriverUser driverUser) {

        driverUser.setGmtCreate(LocalDateTime.now());
        driverUser.setGmtModified(LocalDateTime.now());
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success("");
    }

    /**
     * 查询司机
     *
     * @param driverPhone
     * @return
     */
    public ResponseResult<DriverUser> getUserByPhone(String driverPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("driver_phone", driverPhone);
        map.put("state", DriverCarConstants.DRIVER_STATE_VALID);
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map);
        if (driverUsers.isEmpty()) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXITST.getCode(), CommonStatusEnum.DRIVER_NOT_EXITST.getValue());
        }
        DriverUser driverUser = driverUsers.get(0);
        return ResponseResult.success(driverUser);
    }

    /**
     * 根据车辆id查找订单需要的司机信息
     * @param carId
     * @return
     */
    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId) {
        //车辆和司机绑定关系查询
        LambdaQueryWrapper<DriverCarBindingRelationship> driverCarBindingRelationshipLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getCarId, carId);
        driverCarBindingRelationshipLambdaQueryWrapper.eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND);
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(driverCarBindingRelationshipLambdaQueryWrapper);
        Long driverId = driverCarBindingRelationship.getDriverId();
        //司机工作状态查询
        LambdaQueryWrapper<DriverUserWorkStatus> driverUserWorkStatusLambdaQueryWrapperriverUserWorkStatus = new LambdaQueryWrapper<>();
        driverUserWorkStatusLambdaQueryWrapperriverUserWorkStatus.eq(DriverUserWorkStatus::getDriverId, driverId);
        driverUserWorkStatusLambdaQueryWrapperriverUserWorkStatus.eq(DriverUserWorkStatus::getWorkStatus, DriverCarConstants.DRIVER_WORK_STATUS_START);

        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(driverUserWorkStatusLambdaQueryWrapperriverUserWorkStatus);
        if (null == driverUserWorkStatus) {
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode(), CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        } else {
            //查询司机信息
            LambdaQueryWrapper<DriverUser> driverUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            driverUserLambdaQueryWrapper.eq(DriverUser::getId, driverId);
            DriverUser driverUser = driverUserMapper.selectOne(driverUserLambdaQueryWrapper);
            //查询车辆信息
            LambdaQueryWrapper<Car> carLambdaQueryWrapper = new LambdaQueryWrapper<>();
            carLambdaQueryWrapper.eq(Car::getId,carId);
            Car car = carMapper.selectOne(carLambdaQueryWrapper);

            OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
            orderDriverResponse.setDriverId(driverId);
            orderDriverResponse.setCarId(carId);
            orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());
            orderDriverResponse.setVehicleNo(car.getVehicleNo());
            orderDriverResponse.setLicenseId(driverUser.getLicenseId());
            orderDriverResponse.setVehicleType(car.getVehicleType());

            return ResponseResult.success(orderDriverResponse);
        }

    }
}
